#ifndef POSTBONDDATAANALYSIS_H
#define POSTBONDDATAANALYSIS_H

#include "Calibration/pixel2mech.h"
#include <QMap>
#include <QMutex>
#include <QMutexLocker>
#include <QObject>
#include <QThread>
#include <QVariantMap>

class PostBondDataAnalysis : public QObject
{
    Q_OBJECT

public:
    explicit PostBondDataAnalysis(QObject *parent = nullptr);

    ~PostBondDataAnalysis();

    void appendData(PrOffset &prOffset);

    Q_INVOKABLE int maxData() const
    {
        return NMaxData;
    }

    Q_INVOKABLE int minOffset() const
    {
        return MinOffset;
    }

    Q_INVOKABLE int maxOffset() const
    {
        return MaxOffset;
    }

    Q_INVOKABLE void clearData();

signals:
    void newData(double offsetX, double offsetY);
    void dataDistributionChanged(int xOffsetIndex,
                                 int xOffset,
                                 double xOffsetRatio,
                                 int yOffsetIndex,
                                 int yOffset,
                                 double yOffsetRatio,
                                 double maxRatio);
    void newDataDistribution(QVariantList xAxis,
                             QVariantList yAxisXOffsetDistribution,
                             QVariantList yAxisYOffsetDistribution,
                             double maxRatio);

private:
    int getOffsetRangeIndex(double &offset);
    void notifyDataDistributionChanged(int xOffsetRangeIndex, int yOffsetRangeIndex);

    // For test
    bool isPushingDatas = false;
public slots:
    void pushData();
    void pushDatas(int count);
    void stopPushDatas();

private:
    const int NMaxData = 100;
    const int MinOffset = -30;
    const int MaxOffset = 30;
    const int RangeSize = 5;
    int rangeCount;
    QMutex locker;
    int *everyXOffsetRangeCount;
    int *everyYOffsetRangeCount;
    QList<PrOffset> postBondOffsets;
    double maxOffsetRatio = 0;
};

#endif    // POSTBONDDATAANALYSIS_H
